package com.htandroid.htandroid.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ab.model.AbMenuItem;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.tools.ToolLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ListPopAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Map<AbMenuItem, AbMenuItem>> data = new ArrayList<Map<AbMenuItem, AbMenuItem>>();
    private int itemResource;

    public ListPopAdapter(Context context, ArrayList<Map<AbMenuItem, AbMenuItem>> data, int itemResource) {
        this.context = context;
        this.data = data;
        this.itemResource = itemResource;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(itemResource, null);
            holder = new ViewHolder();
            holder.itemText = (TextView) convertView.findViewById(R.id.tv_text);
            holder.itemData = (TextView) convertView.findViewById(R.id.tv_data);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Map<AbMenuItem, AbMenuItem> item = data.get(position);
        List<String> listText = new ArrayList<>();
        for (AbMenuItem key : item.keySet()) {
            listText.add(key.getText());
        }
        List<String> listData = new ArrayList<>();
        for (AbMenuItem value : item.values()) {
            listData.add(value.getText());
        }
        for (int i = 0; i < item.size(); i++) {
            holder.itemText.setText(listText.get(i));
            holder.itemData.setText(listData.get(i));
        }
        return convertView;
    }

    static class ViewHolder {
        TextView itemText;
        TextView itemData;
    }
}
